Skip to main content

Data Types

Type Categories

In PREDA types could be mainly classified into 2 categories: value type and reference type.

Value types store the data right in the variable, when they are assigned, the data is copied.

bool a = true;
bool b;
b = a; // b get a copy of a's value
b = false; // only b's copy of value is modified, value of a is still "true"

Typical value types are numbers, boolean and enumeration types.

Reference types store a reference to the actual data, when they are assigned, the reference is copied and the data is shared.

struct S{
bool a;
}
S s0;
s0.a = true;
S s1;
s1 = s0; // s1 now holds a reference to the same data as s0's
s1.a = false; // the shared copy of data is modified, hence s0.a is now also "false"

Typical reference types are string, array, map, token and user-defined structures.

Value Types

Built-in Boolean and Integer Types

PREDA has the following built-in boolean and integer types:

typesize in bytesvalue range
bool1true / false
int81[-2^7, 2^7-1]
int162[-2^15, 2^15-1]
int324[-2^31, 2^31-1]
int648[-2^63, 2^63-1]
int12816[-2^127, 2^127-1]
int25632[-2^255, 2^255-1]
int51264[-2^511, 2^511-1]
uint81[0, 2^8-1]
uint162[0, 2^16-1]
uint324[0, 2^32-1]
uint648[0, 2^64-1]
uint12816[0, 2^128-1]
uint25632[0, 2^256-1]
uint51264[0, 2^512-1]
bigintvarying[-2^8128 + 1, 2^8128 - 1]

Supported operators

typesymbolsboolint typesuint typesbigint
assignment=XXXX
logical&&, ||, !X
equality comparison==, !=XXXX
generic comparison<, >, <=, >=XXX
increment and decrement++, --XXX
arithmetic+, -, *, /, %, +=, -=, *=, /=, %=XXX
negation-XX
bitwise~, &, ^, |, <<, >>, &=, ^=, |=, <<=, >>=X

Note: when uint types are shifted more than its bit-width, result would be 0.

Built-in Floating Point Types

PREDA has three types of build-in floating point types: float256, float512 and float1024, with the corresponding bit-width. They support the following operators

typesymbolsfloat256 / float512 / float1024
assignment=X
logical&&, ||, !
equality comparison==, !=X
generic comparison<, >, <=, >=X
increment and decrement++, --
arithmetic+, -, *, /, %, +=, -=, *=, /=, %=X (except modulo)
negation-X
bitwise~, &, ^, |, <<, >>, &=, ^=, |=, <<=, >>=

Enumerations

Enumeration types are defined with a list of enumerators which the value is restricted to.

enum MyEnum{
EnumValueA,
EnumValueB,
...
}
MyEnum e = MyEnum.EnumValueA; // enumerators must be accessed through the enumeration type name

An enumeration type cannot have more than 65535 enumerators.

Other Built-in Value Types

In addition to the types above, PREDA also provides the following built-in fundamental types:

typesize in bytesdescription
blob36digest of a data block
hash32SHA256 hash value
address36an address on the chain

Supported operators

typesymbolsblobhashaddress
assignment=XXX
logical&&, ||, !
equality comparison==, !=XXX
generic comparison<, >, <=, >=XXX
increment and decrement++, --
arithmetic+, -, *, /, %, +=, -=, *=, /=, %=
negation-
bitwise~, &, ^, |, <<, >>, &=, ^=, |=, <<=, >>=

The type address has the following built-in member functions:

functionreturn typeargumentsis constdescription
is_userboolNoneYesif the address is a user address
is_delegatedboolNoneYesif the address is a delegated address
is_dappboolNoneYesif the address is a dapp address
is_assetboolNoneYesif the address is an asset address
is_nameboolNoneYesif the address is a name address
is_contractboolNoneYesif the address is a contract address
is_customboolNoneYesif the address is a custom address

Reference Types

Built-in Generic Containers

typesize in bytesdescription
arrayvaryinga dynamic array of elements of the same type
mapvaryinga mapping from keys to values

Supported operators

typesymbolsarraymap
assignment=XX
logical&&, ||, !
equality comparison==, !=
generic comparison<, >, <=, >=
increment and decrement++, --
arithmetic+, -, *, /, %, +=, -=, *=, /=, %=
negation-
bitwise~, &, ^, |, <<, >>, &=, ^=, |=, <<=, >>=

Dynamic Array : array

An array is an array of dynamic size containing elements of the same type. It supports the bracket operator "[]", the index type must be uint32. The corresponding value type is the first template parameter given at definition, e.g. array\<int64>. array has the following built-in member functions that could be access through the dot operator ".":

functionreturn typeargumentsis constdescription
lengthuint32NoneYesreturns the number of elements in the array
set_lengthNoneuint32 newLengthNoresize the array to newLength. Existing elements are kept. If newLength is larger than current length, elements with default value of valueType are appended.
pushNonevalueType newElementNoappend a new element to the end of the array
popNoneNoneNoremove the last element from the array

Key-Value Map: map

A map is a mapping from keys to values. It supports the bracket operator "[]". The key type and value type are the first and second template parameter given at definition, e.g. map\<address, string>. map has the following built-in member functions that could be access through the dot operator ".":

functionreturn typeargumentsis constdescription
hasboolkeyType keyYesif the key exists in the map
eraseNonekeyType keyNoremove the element that has the given key, if it exists

string

string holds an array of characters in UTF-8 format. Besides assignment ("="), it also supports equality comparison ("==", "!=") and generic comparison ("<", ">", "<=", ">=") operators. string has the following built-in member functions that could be access through the dot operator ".":

functionreturn typeargumentsis constdescription
setNonestring strNoset the content of the string to str.
appendNonestring strNoappend str to the end of the current string
lengthuint16NoneYesget the length of the string

A string can have up to 65535 characters.

token

token is the built-in type for carrying certain amount of tokens that can be also stored in contracts states or carried around in transactions. It doesn't support any operator besides assignment "=" (to copy the reference, since it's a reference type). It has the following built-in functions

functionreturn typeargumentsis constdescription
get_iduint64NoneYesreturns the id of the token stored in token
get_amountbigintNoneYesreturns the amount of token stored in type
transferbooltoken recipient, bigint transfer_amountNotransfers a certain amount of token to another token
transfer_allbooltoken recipientNotransfers all token in the current token to another one

transfer() and transfer_all() would fail if:

  1. the token id is 0, or
  2. the recipient is already holding some token of a different id, or

In addition, transfer() could fail if:

  1. the amount to transfer is negative, or

  2. the token doesn't have sufficient amount to transfer.

Structures

Users can define custom struct types in their code.

A struct is a collection of data grouped together under one name. The members of a struct can be of any built-in type and other user-defined structs.

A struct cannot have member functions.

struct MyStruct{
TypeA memberA;
TypeB memberB;
TypeC memberC;
...
}

The members of a struct can be accessed using the dot operator ".", for example:

// following the definition above
MyStruct myStruct;
myStruct.memberA = ...

A struct cannot have more than 255 members.

Type Conversion

Implicit Conversion

PREDA allows implicit conversion from integers to another integer type with wider range, if both types are signed or unsigned, e.g.

int16 x;
x = 3; // Error: integer literals default to type "int32", which has wider range than int16 . Write x = 3i16 instead.
int8 y;
y = x; // Error: int8 is not a subset of int16. Write y = int16(x) instead.
uint64 v;
int256 u = v; // Error: there is no implicit conversion between signed / unsigned integer types. Write u = int256(v) instead.
uint128 w = v; // Ok: Converting from unsigned 64-bit to unsigned 128-bit, which has a wider range

Explicit Conversion

Explicit type conversion is allowed between

FromToExplanation
any integer typeany integer typeA runtime check will be performed to verify that the source value is inside range of the target type. Otherwise a runtime error will be generated and execution ends immediately
any integer / float typestringConvert the number to a readable string
stringaddressConvert a string in the format of address literal to a string
stringhashCalculate the hash value of a string
hashaddressCreate a custom type address with value equal to the hash
hashstringConvert the hash to a string in the same format as a hash literal, but without ":hash" suffix
addressstringConvert the address to a string in the same format as a address literal

A couple examples:

int16 x = 1000i16;
uint8 y = uint8(x); // Runtime error: 1000 is not within value range of uint8, which is [0, 255]
float256 f = 100.1324;
string s = string(f); // s is now "100.1324"
address a = vffgwr07yq323axszgxbr2qp9azzbyjjm844s90z8ack63s6hrch683z48:ed25519
s = string(a); // s is now "vffgwr07yq323axszgxbr2qp9azzbyjjm844s90z8ack63s6hrch683z48:ed25519"
hash h = 36nwe8x9sig7gb98zkb6gh@qarffhvf6c3ok9433@tz9ne4mb6qi:hash
s = string(h); // s is now "36nwe8x9s1g7gb98zkb6hzzqarffhvf6c30k9433ztz9ne4mb6q0"